package com.qst.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.pojo.Result;
import com.qst.pojo.User;
import com.qst.query.MemberQuery;
import com.qst.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @ClassNmae: MemberController
 * @Author: 邓嘉顺
 * @Description:
 */
@Controller
@RequestMapping("/member")
public class MemberController {
    @Autowired
    MemberService memberService;

    @RequestMapping("/ToMemberInfo")
    @ResponseBody
    public  Result ToMemberInfo(Model model,@ModelAttribute MemberQuery memberQuery) throws JsonProcessingException {
//        改baseQuery来接收前端的limit
        PageHelper.startPage(memberQuery.getPage(),memberQuery.getLimit());
//        这个useList是已经分页过的数据了
        List<User> userList=   memberService.queryAllMember(memberQuery.getUserCard(),memberQuery.getUserName());
        PageInfo<User> pageInfo = new PageInfo<>(userList);
//        在转成data封装，用于前端接收
        List<User> data = pageInfo.getList();
//        要传总数据个数
        int count =  memberService.getAllCount();
        return  new Result(0,"",data,count);
//        return "memberInfo";
    }




    @RequestMapping("/ToMember")
    public  String ToMember() throws JsonProcessingException {
//        List<User> userList=   memberService.queryAllMember(user.getUserCard());
//        ObjectMapper objectMapper = new ObjectMapper();
//        String json = objectMapper.writeValueAsString(userList);
//        model.addAttribute("userList", json);
//        return json;
        return "memberInfo";


    }
    @RequestMapping("/EditMember")
    @ResponseBody
    public Result EditMember(@RequestParam("id") Integer id, @RequestParam("field") String field, @RequestParam("value") String value){
//        转换，将userAge转换为user_age
        String columnName =   field.replaceAll("[A-Z]", "_$0").toLowerCase();    // 数据库中对应的列名
       Result result=memberService.EditMember(id,columnName,value);
       return result;
    }


    @RequestMapping("/addMember")
    @ResponseBody
    public Result addMember(User user){

        Random random = new Random();
        String account1 = "2023";
//        将五个0到9的随机数字拼接成一个字符串,再拼接2023为会员账号account
        for (int i = 0; i < 5; i++) {
            account1 += random.nextInt(10);
        }
        String account =account1;
        //初始密码
        String password = "123456";

//获取当前日期，并转换格式
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String nowDay = simpleDateFormat.format(date);

        user.setCardTime(nowDay);
        user.setUserCard(account);
        user.setUserPwd(password);
        user.setRoleId(1);

        Result result = memberService.addMember(user);
        return result;
    }

    @RequestMapping("/delMember")
    @ResponseBody
    public Result delMember(@RequestParam("ids[]") Integer[] ids){
        Result result = memberService.delBatch(ids);
        return result;
    }

}
